// pages/recommendSong/recommendSong.js
import requst from '../../utils/request'
import pubsub from 'pubsub-js'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        day: '',
        month: '',
        recommendList: [],
        index: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        let userInfo = wx.getStorageSync('userInfo');
        if (!userInfo) {
            wx.showToast({
                title: '请先登录！',
                icon: 'error',
                success: () => {
                    wx.reLaunch({
                        url: '/pages/login/login',
                    });
                }
            });
        }

        //获取数据
        this.getRecommendList();

        this.setData({
            day: new Date().getDate(),
            month: new Date().getMonth() + 1
        });

        //订阅来自songDetail页面发布的消息
        pubsub.subscribe('switchType', (msg, data) => {
            let {
                recommendList,
                index
            } = this.data;
            if (data === 'pre') {
                if (index === 0) {
                    index = recommendList.length;
                }
                index -= 1;
            } else {
                if (index === recommendList.length - 1) {
                    index = -1;
                }
                index += 1;
            }
            this.setData({
                index
            });
            let musicId = recommendList[index].id;
            //将musicId回传给songDetail页面
            pubsub.publish("musicId", musicId);
        });

    },

    async getRecommendList() {
        let result = await requst("/recommend/songs");
        this.setData({
            recommendList: result.recommend
        });
    },

    toSongDetail(event) {
        let song = event.currentTarget.dataset.song;
        let index = event.currentTarget.dataset.index;
        this.setData({
            index
        });

        wx.navigateTo({
            url: '/pages/songDetail/songDetail?musicId=' + song.id,
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})